window.onload=function(){
    var swiper = new Swiper('.swiper-container', {
        effect : 'fade',
        spaceBetween: 30,
        centeredSlides: true,
        loop:true,
        autoplay: {
            delay: 3000,
            disableOnInteraction: false,
        },
        pagination: {
            el: '.swiper-pagination',
            clickable: true,
        }
    });
}
var app=new Vue({
    el:'.mobileapp',
    computed:{
        exchangenum:{//兑换数量
            get(){
                if(!isNaN(Number(this.exchange.exchangenum))){
                    return this.exchange.exchangenum
                }else{
                    return 0
                }
            },
            set(v){ 
                if(!isNaN(Number(v))){
                    if(parseFloat(Number(v))<0){
                        this.exchange.exchangenum=0
                    }else{
                        this.exchange.exchangenum=parseFloat(v)
                    }
                    if(parseFloat(Number(v))>parseFloat(this.userinfo.banlance)){
                        this.exchange.exchangenum=parseInt(this.userinfo.banlance)
                    }
                    let computedresult=parseFloat(Number(this.exchange.exchangenum))*this.exchange.agratenum
                    if(!isNaN(Number(computedresult))){
                        this.exchange.exchangeagcoin=computedresult
                        if(this.exchange.exchangeagcoin > 3000){
                            let lang = this.getCookie("lang");
                            let timer=setInterval(() => {
                                 document.getElementsByClassName('errorMessage')[0].className='errorMessage'
                                 clearInterval(timer)
                                 timer=null
                             },2000)
                             document.getElementsByClassName('errorMessage')[0].className='errorMessage fadeInUp'
                             if (lang == "en_US") {
                                 this.information = "The maximum redemption limit for AG points is 3000"
                             } else if (lang == "zh_TW") {
                                 this.information = "AG分最大兌換額度為3000"
                             } else {
                                 this.information = "AG分最大兑换额度为3000"
                             }
                             return 
                        }
                    }else{
                        this.exchange.exchangeagcoin=0
                    }
                }else{
                    this.cashout.exchangeagcoin=0
                }
            }
        },
        cashoutnum:{ //兑出数量
            get(){
                if(!isNaN(Number(this.cashout.exchangenum))){
                    if(parseFloat(this.cashout.exchangenum)>=0){
                        return this.cashout.exchangenum
                    }else{
                        return 0
                    }
                }else{
                    return 0
                }
            },
            set(v){ 
                if(!isNaN(Number(v))){
                    if(parseFloat(Number(v))<0){
                        this.cashout.exchangenum=0
                    }else{
                        this.cashout.exchangenum=parseInt(v)
                    }
                    if(parseFloat(Number(v))>parseFloat(this.userinfo.myagbanlance)){
                        this.cashout.exchangenum=parseInt(this.userinfo.myagbanlance)
                    }
                    let computedresult=parseFloat(Number(this.cashout.exchangenum))/parseFloat(this.exchange.agratenum)
                    computedresult=parseFloat(computedresult.toFixed(8)) 
                    if(!isNaN(Number(computedresult))){
                        this.cashout.exchangeagcoin=computedresult
                    }else{
                        this.cashout.exchangeagcoin=0
                    }
                }else{
                    this.cashout.exchangeagcoin=0
                }
            }
        },
        sabaexchangenum:{ //沙巴兑换数量
            get(){
                if(!isNaN(Number(this.sabaexchange.exchangenum))){
                    return this.sabaexchange.exchangenum
                }else{
                    return 0
                }
            },
            set(v){ 
                if(!isNaN(Number(v))){
                    if(parseFloat(Number(v))<0){
                        this.sabaexchange.exchangenum=0
                    }else{
                        this.sabaexchange.exchangenum=parseFloat(v)
                    }
                    if(parseFloat(Number(v))>parseFloat(this.sabauserinfo.banlance)){
                        this.sabaexchange.exchangenum=parseInt(this.sabauserinfo.banlance)
                    }
                    let computedresult=parseFloat(Number(this.sabaexchange.exchangenum))*this.sabaexchange.agratenum
                    if(!isNaN(Number(computedresult))){
                        this.sabaexchange.exchangeagcoin=computedresult
                        if(this.sabaexchange.exchangeagcoin > 3000){
                            let lang = this.getCookie("lang");
                            let timer=setInterval(() => {
                                 document.getElementsByClassName('errorMessage')[0].className='errorMessage'
                                 clearInterval(timer)
                                 timer=null
                             },2000)
                             document.getElementsByClassName('errorMessage')[0].className='errorMessage fadeInUp'
                             if (lang == "en_US") {
                                 this.information = "The maximum redemption limit for saba points is 3000"
                             } else if (lang == "zh_TW") {
                                 this.information = "saba分最大兌換額度為3000"
                             } else {
                                 this.information = "saba分最大兑换额度为3000"
                             }
                        }
                    }else{
                        this.sabaexchange.exchangeagcoin=0
                    }
                }else{
                    this.sabacashout.exchangeagcoin=0
                }
            }
        },
        sabacashoutnum:{ //沙巴兑出数量
            get(){
                if(!isNaN(Number(this.sabacashout.exchangenum))){
                    if(parseFloat(this.sabacashout.exchangenum)>=0){
                        return this.sabacashout.exchangenum
                    }else{
                        return 0
                    }
                }else{
                    return 0
                }
            },
            set(v){ 
                if(!isNaN(Number(v))){
                    if(parseFloat(Number(v))<0){
                        this.sabacashout.exchangenum=0
                    }else{
                        this.sabacashout.exchangenum=parseInt(v)
                    }
                    if(parseFloat(Number(v))>parseFloat(this.sabauserinfo.myagbanlance)){
                        this.sabacashout.exchangenum=parseInt(this.sabauserinfo.myagbanlance)
                    }
                    let computedresult=parseFloat(Number(this.sabacashout.exchangenum))/parseFloat(this.sabaexchange.agratenum)
                    computedresult=parseFloat(computedresult.toFixed(8)) 
                    if(!isNaN(Number(computedresult))){
                        this.sabacashout.exchangeagcoin=computedresult
                    }else{
                        this.sabacashout.exchangeagcoin=0
                    }
                }else{
                    this.sabacashout.exchangeagcoin=0
                }
            }
        }
    },
    created(){
        if(localStorage.getItem('ID')!=null||localStorage.getItem('ID')!=''){
            this.getagBalance()
            this.getsabaBalance()
        }
    },
    mounted(){
        this.loading=true
        let test=location.href
        if(test.indexOf("ref")!= -1){
            let ref=test.substring(test.indexOf("ref") + 4)
            window.sessionStorage.setItem('refererId',ref)  
        }
    },
    data(){
        return {
            showtransforminag:true,//兑换AG分
            showtransformout:false,//兑出AG分
            showText: false,
            hideText: true,
            isshowexchange:false, //是否再开兑换兑出功能
            exchange:{ //兑换配置项
                defaultcoin:'HPS', //默认币种
                isshowcoinarr:false, //是否展示下拉选
                exchangenum:'0', //兑换数量
                exchangeagcoin:'0',//兑换后数量
                agratenum:5, //AG兑换比例
                disabledbtn:false //禁用兑换点击按钮
            },
            cashout:{ //兑出配置项
                exchangenum:'0', //兑换数量
                exchangeagcoin:'0',//兑换后币种数量
                disabledbtn:false //禁用兑出点击按钮
            },
            userinfo:{ //用户信息
                banlance:'',//用户余额
                myagbanlance:0,//我的ag积分余额
            },
            information:"",
            loading:false,
            sabashowtransforminag:true,//兑换沙巴分
            sabashowtransformout:false,//兑出沙巴分
            issabashowexchange:false, //是否沙巴兑换兑出功能
            sabaexchange:{ //沙巴兑换配置项
                defaultcoin:'HPS', //默认币种
                isshowcoinarr:false, //是否展示下拉选
                exchangenum:'0', //兑换数量
                exchangeagcoin:'0',//兑换后数量
                agratenum:5, //沙巴兑换比例
                disabledbtn:false //禁用兑换点击按钮
            },
            sabacashout:{ //沙巴兑出配置项
                exchangenum:'0', //兑换数量
                exchangeagcoin:'0',//兑换后币种数量
                disabledbtn:false //禁用兑出点击按钮
            },
            sabauserinfo:{ //沙巴用户信息
                banlance:'',//用户余额
                myagbanlance:0,//我的沙巴积分余额
            }
        }
    },
    methods:{
        gotohyperdice(){ //头部前去dice
            if(localStorage.getItem('ID')!=null||localStorage.getItem('ID')!=''){
                axios.post('/user/setredis',{}).then((res) => {
                    if(res.data.code==200){
                        if(res.data.data!='fail'){
                            let userkey=res.data.data
                            let url='http://dice.play.top/mhome?key='
                            window.open(`${url}${userkey}`,'_self')
                        }else{
                            location.href='/singin'
                        }
                    }else{
                        location.href='/singin'
                    }
                })
            }else{
                location.href='/singin'
            }
        },
        filtertime(num){ //过滤时间
            let times=new Date(num)
            let month=times.getMonth()+1
            let day=times.getDate()
            if(month<10){
                month='0'+month
            }
            if(day<10){
                day='0'+day
            }
            return `${month}-${day}`
        },
        getagBalance(){//获取当前用户AG分
            let params={
                coin:this.exchange.defaultcoin
            }
            axios.post('/ag/agBalance',params).then((res) => {
                if(res.data.code==200){
                    if(res.data.data){
                        this.userinfo.myagbanlance=res.data.data
                    }
                }
            })
        },
        changetransform(keyname){ //用户点击兑换、兑出AG分栏目
            if(!keyname){
                return 
            }
            this.$data[keyname]=!this.$data[keyname]
            if(keyname=='showtransforminag'){ //兑换AG分
                this.showtransformout=false
                if(this.$data[keyname]){
                    this.getagDict(this.exchange.defaultcoin) //根据币种换算兑换比例
                }
            }else if(keyname=='showtransformout'){//兑出AG分
                this.showtransforminag=false
            }else if(keyname=='sabashowtransforminag'){ //兑换沙巴分
                this.sabashowtransformout=false
                if(this.$data[keyname]){
                    this.getsabaDict(this.sabaexchange.defaultcoin) //根据币种换算
                }
            }else if(keyname=='sabashowtransformout'){
                this.sabashowtransforminag=false
            }
        },
        changeexchangecoin(cointype,banlance){//用户点击更换币种
            this.exchange.defaultcoin=cointype
            this.exchange.isshowcoinarr=false
            this.userinfo.banlance=banlance
            this.getagDict(cointype)
            this.getagBalance()
        },
        getagDict(coinname){ //选择币种获取兑换比例
            if(!coinname){
                return 
            }
            axios.get(`/ag/dict/${coinname}`,{}).then((res) => {
                this.exchange.agratenum=parseFloat(res.data.data.dictValue)
            })
        },
        confirmexchange(){ //确定兑换AG分
            //为空、NaN、兑换数量>用户余额
            if(
                this.exchange.exchangenum==''||isNaN(Number(this.exchange.exchangenum))||this.exchange.exchangenum==0||
                parseFloat(Number(this.exchange.exchangenum))>parseFloat(this.userinfo.banlance)
            ){
                return 
            }
            this.exchange.disabledbtn=true
            let params={
                coin:this.exchange.defaultcoin,      
                amount:parseFloat(this.exchange.exchangenum)
            }
            axios.post('/ag/account/buyAgCredit',params).then((res) => {
                this.exchange.disabledbtn=false
                if(res.data.code==200){
                    if(!this.getCookie('firsttransforminag')){
                        this.addCookie('firsttransforminag',new Date().getTime(),'Infinity')
                    }
                    this.getagBalance()
                }
            })
        },
        getCookie(cookiename){ //获取cookie
            var arr,reg=new RegExp("(^| )"+cookiename+"=([^;]*)(;|$)");
            if(arr=document.cookie.match(reg)){
                return unescape(arr[2]);
            }else{
                return null;
            }
        },
        addCookie(objName,objValue,objDays){ //设置cookie
            var str = objName + "=" + escape(objValue);
            if(objDays > 0){
                var date = new Date();
                var ms = objDays*24*3600*1000;
                date.setTime(date.getTime() + ms);
                str += "; expires=" + date.toGMTString();
            }
            if(objDays===Infinity){
                str += "; expires=Fri, 31 Dec 9999 23:59:59 GMT";
            }
            str += "; path=/";
            document.cookie = str;
        },
        enteraggame(gameType=null){ //点击进入游戏
            //无token||无ID
            if(
                // isNaN(Number(this.userinfo.myagbanlance))||
                // parseFloat(this.userinfo.myagbanlance)<=0||
                localStorage.getItem('mytoken')==null||
                localStorage.getItem('mytoken')==''||
                localStorage.getItem('ID')==''||
                localStorage.getItem('ID')==''
            ){
                location.href = '/singin'
                return 
            }
            // 我的ag积分余额小于0||非数字||
            if( 
                isNaN(Number(this.userinfo.myagbanlance))||
                parseFloat(this.userinfo.myagbanlance)<=0
            ){
               let lang = this.getCookie("lang");
               let timer=setInterval(() => {
                    document.getElementsByClassName('errorMessage')[0].className='errorMessage'
                    clearInterval(timer)
                    timer=null
                },2000)
                document.getElementsByClassName('errorMessage')[0].className='errorMessage fadeInUp'
                if (lang == "en_US") {
                    this.information = "Please recharge AG points"
                } else if (lang == "zh_TW") {
                    this.information = "請充值AG分"
                } else {
                    this.information = "请充值AG分"
                }
                return 
            }
            let params={
                coin:this.exchange.defaultcoin,
                gameType:gameType?gameType:8, //AG大厅
                token:localStorage.getItem('mytoken'), //用户令牌
                isMobile:false //pc
            }
            axios.post('/ag/getGameUrl',params).then((res) => {
                if(res.data.code==200){
                    let form=document.createElement('form')
                    form.setAttribute('method','post')
                    form.setAttribute('class','gotoagGame')
                    form.setAttribute('action',res.data.data)
                    // form.setAttribute('target','_blank')
                    document.body.appendChild(form)
                    document.getElementsByClassName('gotoagGame')[0].submit()
                    document.body.removeChild(document.getElementsByClassName('gotoagGame')[0])
                }
            })
        },
        confircashout(){ //从AG兑回
            if(
                localStorage.getItem('ID')==''||
                localStorage.getItem('ID')==''
            ){
                location.href = '/singin'
                return 
            }
            if(
                this.userinfo.myagbanlance==''||
                this.cashout.exchangenum==0||
                this.cashout.exchangenum==''||
                isNaN(Number(this.userinfo.myagbanlance))||
                parseFloat(this.userinfo.myagbanlance)<=0
            ){
                return 
            }
            this.cashout.disabledbtn=true
            let params={
                coin:this.exchange.defaultcoin,
                amount:parseFloat(this.cashout.exchangenum) 
            }
            axios.post('/ag/transferFromAg',params).then((res) => {
                this.cashout.disabledbtn=false
                if(res.data.code==200){
                    if(res.data.data){
                        this.getagBalance()
                    }
                }
            })
        },
        REturnTOp(){
            let BODY =document.getElementsByTagName("body")[0]
            BODY.style.overflowY="hidden"
            let scrollToptimer=setInterval(function () {
                var top = document.body.scrollTop || document.documentElement.scrollTop;
                var speed = top / 4;
                if (document.body.scrollTop!=0) {
                    document.body.scrollTop -= speed;
                }else {
                    document.documentElement.scrollTop -= speed;
                }
                if (top == 0) {
                    clearInterval(scrollToptimer);
                    let BODY =document.getElementsByTagName("body")[0]
                    BODY.style.overflowY=""
                }
            }, 30); 
        },
        xxtea_encrypt(str, key) {
            if (str == "") {
                return "";
            }
            var v = this.str2long(str, true);
            var k = this.str2long(key, false);
            if (k.length < 4) {
                k.length = 4;
            }
            var n = v.length - 1;
          
            var z = v[n], y = v[0], delta = 0x9E3779B9;
            var mx, e, p, q = Math.floor(6 + 52 / (n + 1)), sum = 0;
            while (0 < q--) {
                sum = sum + delta & 0xffffffff;
                e = sum >>> 2 & 3;
                for (p = 0; p < n; p++) {
                    y = v[p + 1];
                    mx = (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
                    z = v[p] = v[p] + mx & 0xffffffff;
                }
                y = v[0];
                mx = (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
                z = v[n] = v[n] + mx & 0xffffffff;
            }
            return this.long2str(v, false);
        },
        long2str (v, w) {
            var vl = v.length;
            var n = (vl - 1) << 2;
            if (w) {
                var m = v[vl - 1];
                if ((m < n - 3) || (m > n)) return null;
                n = m;
            }
            for (var i = 0; i < vl; i++) {
                v[i] = String.fromCharCode(v[i] & 0xff,
                                           v[i] >>> 8 & 0xff,
                                           v[i] >>> 16 & 0xff,
                                           v[i] >>> 24 & 0xff);
            }
            if (w) {
                return v.join('').substring(0, n);
            }
            else {
                return v.join('');
            }
        },
        str2long(s, w) {
            var len = s.length;
            var v = [];
            for (var i = 0; i < len; i += 4) {
                v[i >> 2] = s.charCodeAt(i)
                          | s.charCodeAt(i + 1) << 8
                          | s.charCodeAt(i + 2) << 16
                          | s.charCodeAt(i + 3) << 24;
            }
            if (w) {
                v[v.length] = len;
            }
            return v;
        },
        changesabaexchangecoin(cointype,banlance){ //兑换沙巴切换币种
            this.sabaexchange.defaultcoin=cointype
            this.sabaexchange.isshowcoinarr=false
            this.sabauserinfo.banlance=banlance
            this.getsabaDict(cointype)
            this.getsabaBalance()
        },
        getsabaDict(coinname){ //请求沙巴兑换比例
            if(!coinname){
                return 
            }
            axios.get(`/ag/dict/${coinname}`,{}).then((res) => {
                if(res.data.data&&res.data.data.dictValue){
                    this.sabaexchange.agratenum=parseFloat(res.data.data.dictValue)
                }else{
                    this.sabaexchange.agratenum=5
                }
            })
        },
        getsabaBalance(){ //请求沙巴余额
            let params = {
                coin: this.sabaexchange.defaultcoin
            }
            axios.post('/sabauserBalance',params).then((res) => {
                if(res.data.code==200){
                    if(res.data.data&&res.data.data.Data.length){
                        this.sabauserinfo.myagbanlance = res.data.data.Data[0].balance
                    }else{
                        this.sabauserinfo.myagbanlance = 0
                    }
                }
            })
        },
        confirmsabaexchange(){ //确认兑换沙巴积分
            if(
                this.sabaexchange.exchangenum==''||
                isNaN(Number(this.sabaexchange.exchangenum))||
                this.sabaexchange.exchangenum==0||
                parseFloat(Number(this.sabaexchange.exchangenum))>parseFloat(this.sabauserinfo.banlance)
            ){
                return 
            }
            this.sabaexchange.disabledbtn=true
            let params={
                coin:this.sabaexchange.defaultcoin,      
                amount:parseFloat(this.sabaexchange.exchangenum),
                direction:1
            }
            axios.post('/sabafundTransfer',params).then( (res) => {
                this.sabaexchange.disabledbtn = false
                if(res.data.code==200){
                    if(res.data.data&&res.data.data.Data){
                        let dialogopt={
                            zh_TW:{
                                title1:'兌換成功',
                                title2:'saba積分兌換成功！'
                            },
                            zh_CN:{
                                title1:'兑换成功',
                                title2:'saba积分兑换成功！'
                            },
                            en_US:{
                                title1:'Successfully redeemed',
                                title2:'Successfully converted the saba points.'
                            }
                        }
                        this.opendialog(dialogopt)
                        this.sabauserinfo.myagbanlance=res.data.data.Data.after_amount
                    }else{
                        let dialogopt={
                            zh_TW:{
                                title1:'提示',
                                title2:'請稍後重試！'
                            },
                            zh_CN:{
                                title1:'提示',
                                title2:'请稍后重试！'
                            },
                            en_US:{
                                title1:'Prompt',
                                title2:'Please try again later!'
                            }
                        }
                        this.opendialog(dialogopt)
                    }
                }else{
                    let dialogopt={
                        zh_TW:{
                            title1:'提示',
                            title2:'請稍後重試！'
                        },
                        zh_CN:{
                            title1:'提示',
                            title2:'请稍后重试！'
                        },
                        en_US:{
                            title1:'Prompt',
                            title2:'Please try again later!'
                        }
                    }
                    this.opendialog(dialogopt)
                }
                this.issabashowexchange=false
            })
        },
        opendialog(obj){ //打开下拉提示
            let lang = this.getCookie("lang");
            let timer=setInterval(() => {
                document.getElementsByClassName('errorMessage')[0].className='errorMessage'
                clearInterval(timer)
                timer=null
            },2000)
            document.getElementsByClassName('errorMessage')[0].className='errorMessage fadeInUp'
            if (lang == "en_US") {
                this.information = obj.en_US.title2
            } else if (lang == "zh_TW") {
                this.information = obj.zh_TW.title2
            } else if (lang == 'zh_CN') {
                this.information = obj.zh_CN.title2
            }
        },
        confirmsabacashout(){ //确认兑回沙巴彩票
            if(
                localStorage.getItem('ID')==''||
                localStorage.getItem('ID')==''
            ){
                location.href = '/singin'
                return 
            }
            if(
                this.sabauserinfo.myagbanlance==''||
                this.sabacashout.exchangenum==0||
                this.sabacashout.exchangenum==''||
                isNaN(Number(this.sabauserinfo.myagbanlance))||
                parseFloat(this.sabauserinfo.myagbanlance)<=0
            ){
                return 
            }
            this.sabacashout.disabledbtn=true
            let params = {
                coin: this.sabaexchange.defaultcoin,
                amount:parseFloat(this.sabacashout.exchangenum),
                direction:0
            }
            axios.post('/sabafundTransfer',params).then( (res) => {
                this.sabacashout.disabledbtn = false
                if(res.data.code==200){
                    if(res.data.data.Data){
                        let dialogopt={
                            zh_TW:{
                                title1:'兌出成功',
                                title2:'saba積分兌出成功！'
                            },
                            zh_CN:{
                                title1:'兑出成功',
                                title2:'saba积分兑出成功！'
                            },
                            en_US:{
                                title1:'Successfully redeemed',
                                title2:'Saba points out successful.'
                            }
                        }
                        this.opendialog(dialogopt)
                        this.sabauserinfo.myagbanlance=res.data.data.Data.after_amount
                    }else{
                        let dialogopt={
                            zh_TW:{
                                title1:'提示',
                                title2:'請稍後重試！'
                            },
                            zh_CN:{
                                title1:'提示',
                                title2:'请稍后重试！'
                            },
                            en_US:{
                                title1:'Prompt',
                                title2:'Please try again later!'
                            }
                        }
                        this.opendialog(dialogopt)
                    }
                }else{
                    let dialogopt={
                        zh_TW:{
                            title1:'提示',
                            title2:'請稍後重試！'
                        },
                        zh_CN:{
                            title1:'提示',
                            title2:'请稍后重试！'
                        },
                        en_US:{
                            title1:'Prompt',
                            title2:'Please try again later!'
                        }
                    }
                    this.opendialog(dialogopt)
                }
                this.issabashowexchange=false
            })
        },
        gotomPicksPal(){ //前往PC端
            if(
                this.sabauserinfo.myagbanlance==0||
                this.sabauserinfo.myagbanlance<0||
                this.sabauserinfo.myagbanlance==''
            ){
                let dialogopt={
                    zh_TW:{
                        title1:'提示',
                        title2:'請先兌換沙巴積分！'
                    },
                    zh_CN:{
                        title1:'提示',
                        title2:'请先兑换沙巴积分！'
                    },
                    en_US:{
                        title1:'Successfully redeemed',
                        title2:'Please exchange for saba points first!'
                    }
                }
                this.opendialog(dialogopt)
                return
            }
            location.href='/m/PicksPal'
        }
    }
})

